package cn.com.sealevel.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/* loaded from: netty-mqtt-client-1.0.0.jar:cn/com/sealevel/util/DataFileWriter.class */
public class DataFileWriter {
    private static final Logger logger = LoggerFactory.getLogger(DataFileWriter.class);
    private String fileName;
    private OutputStream output;

    public DataFileWriter(String fileName) {
        this.fileName = fileName;
    }

    public synchronized void write(String data) {
        if (data != null && !data.isEmpty()) {
            try {
                if (this.output == null) {
                    File dir = new File(ConfigUtil.localDir);
                    if (!dir.exists()) {
                        dir.mkdirs();
                        logger.info("LocalDir={} is not exist, create it.", ConfigUtil.localDir);
                    }
                    this.output = new FileOutputStream(new File(dir, this.fileName));
                }
                this.output.write(data.getBytes());
            } catch (IOException e) {
                logger.error("Write local dir [{}] failed, the reason is {}", ConfigUtil.localDir, e.getMessage());
            }
        }
    }

    public synchronized void close() {
        if (this.output != null) {
            try {
                this.output.close();
            } catch (IOException e) {
            }
        }
    }

    public String getFilePath() {
        return ConfigUtil.localDir + "/" + this.fileName;
    }
}
